package com.pro.controller;

import com.google.gson.Gson;
import com.pro.pojo.*;
import com.pro.service.JsPeopleListService;
import com.pro.utils.MD5Util;
import com.pro.utils.RedisCache;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ClassName:JsPeopleListController
 * Package:com.pro.controller
 * Description:
 *
 * @Date:2021/11/15 16:07
 * @Author: XJR
 */
@RestController
@RequestMapping("/JsPeopleList")
public class JsPeopleListController {
    @Autowired
    private JsPeopleListService service;

    @Autowired
    private RedisCache redisCache;

    private Gson gson = new Gson();

    //查看部门名称
    @RequestMapping("/selectJsDeptName")
    public JsonData selectJsDeptName(){
        List<JsDept> list = service.selectJsDeptName();
        return list!=null?JsonData.buildSuccess(list):JsonData.buildError("服务端错误");
    };

    //查询后台用户信息
    @RequestMapping("/selectJsUser")
    public JsonData selectJsUser(String account, String trueName, String deptName, String registerDate,Integer limit,Integer page){
        account = !("").equals(account) && account !=null?"%"+account+"%":"%%";
        trueName = !("").equals(trueName) && trueName !=null?"%"+trueName+"%":"%%";
        deptName = !("").equals(deptName) && deptName !=null?"%"+deptName+"%":"%%";
        registerDate = !("").equals(registerDate) && registerDate !=null?"%"+registerDate+"%":"%%";
        List<JsUser> jsUsers = service.selectJsUser(account, trueName, deptName, registerDate,limit,page);
        Integer integer = service.countJsUser(account, trueName, deptName, registerDate);

        //redis缓存
        String jsPeopleListSelectJsUser = redisCache.getCacheObject("JsPeopleListSelectJsUser");
        List list = gson.fromJson(jsPeopleListSelectJsUser, List.class);
        Integer integer1 = redisCache.getCacheObject("JsPeopleListCountJsUser");
        return list != null ? JsonData.buildSuccess(list, integer1): JsonData.buildError("服务端错误");
    };
    JsClosure jsClosure = new JsClosure();
    //更改账号状态
    @RequestMapping("/updateStatus")
    public JsonData updateStatus(JsUser jsUser){
        Boolean aBoolean = service.updateStatus(jsUser);
        Integer status = jsUser.getStatus();
        Integer enabled = jsUser.getEnabled();
        Boolean aBoolean1=false;
        Boolean b=false;

        if(status==1&&enabled==1){
            System.out.println(jsClosure);
            aBoolean1 = service.insertUnseal(jsUser.getUserId(),jsUser.getType(),jsUser.getUnsealDate());
            Boolean aBoolean2 = service.updateUserEndDate(jsClosure.getId(),jsUser.getUserId(), jsUser.getUnsealDate());
            if(aBoolean&&aBoolean1&&aBoolean2!=false){
                b=true;
            }
        }else{
            jsClosure.setUserId(jsUser.getUserId());
            jsClosure.setType(3);
            jsClosure.setRemarks(jsUser.getRemark());
            jsClosure.setStartDatee(jsUser.getUnsealDate());
            jsClosure.setEndDatee(jsUser.getEndDate());
            jsClosure.setDays(36524);
            jsClosure.setRemarks("紧急封停");
            Boolean aBoolean2 = service.insertUserClosure(jsClosure);
            if(aBoolean&&aBoolean2!=false){
                b=true;
            }
        }
        return b != false ? JsonData.buildSuccess(b): JsonData.buildError("服务端错误");
    };

    //高级检索
    @RequestMapping("/selectSearchJsUser")
    public JsonData selectSearchJsUser(String email, String phone, String idCard, String status, String sex, String enabled,Integer limit,Integer page){
        email = !("").equals(email) && email !=null?"%"+email+"%":"%%";
        phone = !("").equals(phone) && phone !=null?"%"+phone+"%":"%%";
        idCard = !("").equals(idCard) && idCard !=null?"%"+idCard+"%":"%%";
        status = !("").equals(status) && status !=null?"%"+status+"%":"%%";
        sex = !("").equals(sex) && sex !=null?"%"+sex+"%":"%%";
        enabled = !("").equals(enabled) && enabled !=null?"%"+enabled+"%":"%%";
        List<JsUser> jsUsers = service.selectSearchJsUser(email, phone, idCard, status, sex, enabled,limit,page);
        Integer integer = service.countSearchJsUser(email, phone, idCard, status, sex, enabled);
        return jsUsers != null ? JsonData.buildSuccess(jsUsers,integer): JsonData.buildError("服务端错误");
    };

    //通过id查询用户数据详情
    @RequestMapping("/selectJsUserById")
    public JsonData selectJsUserById(Integer id){
        JsUser jsUser = service.selectJsUserById(id);
        Integer integer = service.countJsUserById(id);
        return jsUser!=null?JsonData.buildSuccess(jsUser,integer) : JsonData.buildError("服务端错误");
    };

    //通过id查看用户的权限
    @RequestMapping("/selectUserSettings")
    public JsonData selectUserSettings(Integer userId){
        List<JsUser> jsUsers = service.selectUserSettings(userId);
        return jsUsers!=null?JsonData.buildSuccess(jsUsers) : JsonData.buildError("服务端错误");
    };

    //对后台用户账户进行封停
    @RequestMapping("/insertUserClosure")
    public JsonData insertUserClosure(JsClosure jsClosure){
        Boolean aBoolean1 = service.updateUserStatus(jsClosure.getUserId(), jsClosure.getStatus(),jsClosure.getEnabled());
        Boolean aBoolean = service.insertUserClosure(jsClosure);
        Boolean a=false;
        if(aBoolean1!=false&&aBoolean!=false){
            a = true;
        }
        return a!=false?JsonData.buildSuccess(a): JsonData.buildError("服务端错误");
    };
    //对后台用户账户进行编辑
    @RequestMapping("/updateUser")
    public JsonData updateUser(JsUser jsUser){
        Boolean aBoolean = service.updateUser(jsUser);
        return aBoolean!=false?JsonData.buildSuccess(aBoolean): JsonData.buildError("服务端错误");
    };

    //添加后台管理人员
    @RequestMapping("/addUser")
    public JsonData addUser(JsUser jsUser){

        //获取的是本地的IP地址
        InetAddress address = null;
        String hostAddress= "";
        try{
            address = InetAddress.getLocalHost();
            hostAddress = address.getHostAddress();
        }catch (Exception e){
            e.printStackTrace();
        }
        jsUser.setRegisterIp(hostAddress);
        String password = MD5Util.md5(jsUser.getPassword());
        jsUser.setPassword(password);
        Boolean aBoolean = service.addUser(jsUser);

        Boolean aBoolean1 = addUserStatus(jsUser);

        Boolean b = false;
        if(aBoolean&&aBoolean1!=false){
            b=true;
        }
       return b!=false?JsonData.buildSuccess(b): JsonData.buildError("服务端错误");
    };

    @RequestMapping("/addUserStatus")
    public Boolean addUserStatus(JsUser jsUser){
        System.out.println(jsUser);
        Integer[] authorityIdd = jsUser.getAuthorityIdd();
        List<JsUser> list = new ArrayList();
        for (int i = 0; i <authorityIdd.length ; i++) {
            JsUser jsUser1 = new JsUser();
            jsUser1.setUserId(jsUser.getUserId());
            jsUser1.setAuthorityId(authorityIdd[i]);
            jsUser1.setType(jsUser.getType());
            jsUser1.setCreateId(jsUser.getCreateId());
            jsUser1.setCreateDate(jsUser.getCreateDate());
            list.add(jsUser1);
        }
        Boolean aBoolean1 = service.addUserStatus(list);
        return aBoolean1;
    };

    //删除后台权限
    @RequestMapping("/delUserStatus")
    public Boolean delUserStatus(Integer userId){
        Boolean aBoolean = service.delUserStatus(userId);
        return aBoolean;
    };
}
